package net.runman.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

/**
 * Web相关配置
 * @author L.cm
 * @email: 596392912@qq.com
 * @site: http://www.dreamlu.net
 * @date 2015年5月3日下午12:52:33
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

	// druid监控用户名
	@Value("${druid.username}")
	private String druidUsername;
	// druid监控密码
	@Value("${druid.password}")
	private String druidPassword;

	/**
	 * 注册druid监控Servlet
	 * @return ServletRegistrationBean
	 */
	@Bean
	public ServletRegistrationBean druidStatViewServletBean(){
		ServletRegistrationBean druidStatViewServletBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
		druidStatViewServletBean.addInitParameter("loginUsername", druidUsername);
		druidStatViewServletBean.addInitParameter("loginPassword", druidPassword);
		return druidStatViewServletBean;
	}

	/**
	 * 注册druid监控Filter
	 * @return FilterRegistrationBean
	 */
	@Bean
	public FilterRegistrationBean druidWebStatFilterBean(){
		FilterRegistrationBean druidWebStatFilterBean = new FilterRegistrationBean();
		druidWebStatFilterBean.setFilter(new WebStatFilter());
		druidWebStatFilterBean.addUrlPatterns("/*");
		druidWebStatFilterBean.addInitParameter("exclusions", "/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
		druidWebStatFilterBean.addInitParameter("sessionStatEnable", "false");
		return druidWebStatFilterBean;
	}

	/**
	 * 直接访问jsp的路由
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/404.html").setViewName("error/404");
		registry.addViewController("/500.html").setViewName("error/500");
	}

}
